package com.rp.tag;

import java.io.IOException;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.rp.common.utils.JsonUtil;
import com.rp.system.po.Button;

public class ButtonTag extends TagSupport{

	private static final long serialVersionUID = 6880128192743012317L;
	
	private String buttons;//按钮数组
	private String type;//需要显示的按钮的类型
	
	@Override
	public int doEndTag() throws JspException {
		JspWriter out = pageContext.getOut();
		try{
			//System.out.println(buttons);
			//System.out.println(type);
			String btnText = "无匹配类型的按钮";
			try {
				List<Button> btns = JsonUtil.jsonToList(buttons, "com.rp.system.po.Button");
				for (Button btn : btns) {
					if(btn.getType().equals(type)){
						btnText = btn.getText();
						break;
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			out.print(btnText);
		}catch (IOException e) {
			throw new JspException(e);
		}
		return EVAL_PAGE;
	}

	public String getButtons() {
		return buttons;
	}

	public void setButtons(String buttons) {
		this.buttons = buttons;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}
	
}
